package com.five.welcome.utils;

import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Base64;
@Component
public class MultipartFileToBase64Util {

    public  String generateBase64(MultipartFile file) {
        if (file == null || file.isEmpty()) {
            throw new RuntimeException("图片不能为空！");
        }
       /* String contentType = file.getContentType();*/
        String base64EncoderImg = "";
        try {
            byte[] fileBytes = file.getBytes();
            /*base64EncoderImg = "data:" + contentType + ";base64," + Base64.getEncoder().encodeToString(fileBytes);
            base64EncoderImg = base64EncoderImg.replaceAll("[\\s*\t\n\r]", "");*/
            base64EncoderImg=Base64.getEncoder().encodeToString(fileBytes);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return base64EncoderImg;
    }
}
